package com.fenqing.comics.dao.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fenqing.comics.dao.ICrawlerTaskHistoryDao;
import com.fenqing.comics.dao.mapper.CrawlerTaskHistoryMapper;
import com.fenqing.comics.entity.CrawlerTaskHistoryEntity;
import com.fenqing.comics.enums.CrawlerTaskHistoryEnums;
import org.springframework.stereotype.Repository;

/**
 * 爬虫任务历史表(CrawlerTaskHistory)表数据库访问层
 * @author fenqing
 */
@Repository
public class CrawlerTaskHistoryDaoImpl
        extends ServiceImpl<CrawlerTaskHistoryMapper, CrawlerTaskHistoryEntity>
        implements ICrawlerTaskHistoryDao {

    @Override
    public CrawlerTaskHistoryEntity getBySourceId(Long sourceId) {
        return this.lambdaQuery()
                .eq(CrawlerTaskHistoryEntity::getSourceId, sourceId)
                .last("limit 1")
                .one();
    }

    @Override
    public CrawlerTaskHistoryEntity getLastBySourceId(Long sourceId) {
        return this.lambdaQuery()
                .eq(CrawlerTaskHistoryEntity::getSourceId, sourceId)
                .orderByDesc(CrawlerTaskHistoryEntity::getTime)
                .last("limit 1")
                .one();
    }

    @Override
    public CrawlerTaskHistoryEntity getLastBySourceId(Long sourceId, CrawlerTaskHistoryEnums.Type type) {
        return this.lambdaQuery()
                .eq(CrawlerTaskHistoryEntity::getSourceId, sourceId)
                .eq(CrawlerTaskHistoryEntity::getType, type)
                .orderByDesc(CrawlerTaskHistoryEntity::getTime)
                .last("limit 1")
                .one();
    }

}
